package com.yj.rabbitClient.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ReturnListener;
import com.yj.rabbitClient.util.RabbitMQUtil;

import java.io.IOException;

/**
 *
 */
public class Publisher02 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtil.getConnection();

        Channel channel = connection.createChannel();
        String exchangeName = "hello";
        channel.exchangeDeclare(exchangeName,"fanout");
        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(replyCode+";"+replyText+";"+exchange+";"+routingKey+";"+properties+";"+new String(body));
            }
        });
        // 第三个参数是 mandatory 属性，表示是否开启消息返回机制，如果这个属性被置为 false ，则消息返回监听器就不会生效。
        channel.basicPublish(exchangeName,"helloworld1",true,null,"helloworld".getBytes());
    }
}
